﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SistemaAcademicoBO;
using System.Configuration;
using System.Data;
using SistemaAcademicoBO.Entities;

namespace SistemaAcademicoDAL
{
    public class SubjectInDegreeCourseDAO : AbstratcDAO, IDAO<IEntity>
    {
        public SubjectInDegreeCourseDAO()
        {
            connection = new Connection();
        }

        public IEntity GetRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveSubjectInDegreeCourse", connection.GetSQLConnection());

            procedureCall.Parameters.Add(new ProcedureParam("@SubjectInDegreeCourseId", SqlDbType.Int, 0, id));

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(SubjectInDegreeCourse));

            connection.CloseConnection();

            if (resultSet.Count != 1)
                return null;

            return resultSet[0];
        }

        public List<IEntity> GetAllRecords()
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_retrieveAllSubjectsInDegreeCourse", connection.GetSQLConnection());

            procedureCall.Execute(true);

            List<IEntity> resultSet = procedureCall.ExtractFromResultSet(typeof(SubjectInDegreeCourse));

            connection.CloseConnection();

            return resultSet;
        }

        public void SetRecord(IEntity entity)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();
            ProcedureCall procedureCall = new ProcedureCall("pr_storeSubjectInDegreeCourse", connection.GetSQLConnection());

            SubjectInDegreeCourse subjectInDegreeCourse = (SubjectInDegreeCourse)entity;

            procedureCall.Parameters.Add(new ProcedureParam("@SubjectId", SqlDbType.Int, 0, subjectInDegreeCourse.SubjectId));
            procedureCall.Parameters.Add(new ProcedureParam("@ProfessorId", SqlDbType.Int, 0, subjectInDegreeCourse.ProfessorId));
            procedureCall.Parameters.Add(new ProcedureParam("@DegreeCourseId", SqlDbType.VarChar, 100, subjectInDegreeCourse.DegreeCourseId));
            procedureCall.Parameters.Add(new ProcedureParam("@SubjectInDegreeCourseId", SqlDbType.VarChar, 100, subjectInDegreeCourse.SubjectInDegreeCourseId));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }

        public void RemoveRecord<K>(K id)
        {
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            connection.OpenConnection();

            ProcedureCall procedureCall = new ProcedureCall("pr_removeSubjectInDegreeCourse", connection.GetSQLConnection());

            procedureCall.Parameters.Add(new ProcedureParam("@SubjectInDegreeCourseId", SqlDbType.Int, 0, id));

            procedureCall.Execute(false);

            connection.CloseConnection();
        }
    }
}
